﻿Public Class frqlthucodinh
    Dim DS As New List(Of ThuCoDinh)

    Sub HienThi()
        DS = XL_ThuCoDinh.LayDS(Me.Tag)
        lsvphieuthu.Items.Clear()
        For Each p In DS
            Dim li As ListViewItem = New ListViewItem(p.Ngay)
            li.Tag = p
            li.SubItems.Add(p.SoTien)
            li.SubItems.Add(XL_LoaiThu.Lay(p.LoaiThu).Loai)
            li.SubItems.Add(XL_TaiKhoan.Lay(p.TaiKhoan).TenTK)
            li.SubItems.Add(p.LanCuoi)
            lsvphieuthu.Items.Add(li)
        Next
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Dim f As New fr_Loaithu
        f.ShowDialog()
    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Me.Close()
    End Sub

    Private Sub btnthemqlthucodinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthemqlthucodinh.Click
        Try
            Dim p As New ThuCoDinh
            p.Ngay = Integer.Parse(txt_ngaythu.Text)
            p.SoTien = Integer.Parse(txtsotienpt.Text)
            p.LoaiThu = cbxloaithunhappt.SelectedValue
            p.TaiKhoan = cbxtaikhoanpt.SelectedValue
            p.NguoiThu = Me.Tag
            p.ChiTiet = rtbchitiet.Text
            p.LanCuoi = New Date(Now.Year, Now.Month - 1, p.Ngay)
            XL_ThuCoDinh.Them(p)
            Dim count = XL_ThuCoDinh.TuDongPhatSinh(Me.Tag)
            MessageBox.Show("Phát sinh " + count.ToString() + " phiếu thu tự động", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            HienThi()
        Catch ex As Exception
            MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End Try
    End Sub

    Private Sub tbnsuaqlthucodinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbnsuaqlthucodinh.Click
        If (lsvphieuthu.SelectedItems.Count > 0) Then
                Try
                    Dim p As ThuCoDinh = lsvphieuthu.SelectedItems(0).Tag
                    p.Ngay = Integer.Parse(txt_ngaythu.Text)
                    p.SoTien = Integer.Parse(txtsotienpt.Text)
                    p.LoaiThu = cbxloaithunhappt.SelectedValue
                    p.TaiKhoan = cbxtaikhoanpt.SelectedValue
                    p.ChiTiet = rtbchitiet.Text
                    Dim kq As Integer = XL_ThuCoDinh.CapNhat(p)
                    If kq <> 0 Then
                        HienThi()
                        MessageBox.Show("Cập nhật thành công!")
                    Else
                        MessageBox.Show("Cập nhật không thành công!")
                    End If
                Catch ex As Exception
                    MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Exit Sub
                End Try
        Else
            MessageBox.Show("Chưa chọn vị trí cập nhật!", "Chú ý!")
        End If
    End Sub

    Private Sub btnxoaqlthucodinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoaqlthucodinh.Click
        If (lsvphieuthu.SelectedItems.Count > 0) Then
            If MessageBox.Show("Ban co muon xoa vi tri da chon ?", "Chu y!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                Try
                    Dim p As ThuCoDinh = lsvphieuthu.SelectedItems(0).Tag
                    Dim kq As Integer = XL_ThuCoDinh.Xoa(p)
                    If kq <> 0 Then
                        HienThi()
                        MessageBox.Show("Xoa thanh cong!")
                    Else
                        MessageBox.Show("Xoa khong thanh cong")
                    End If

                Catch ex As Exception
                    Exit Sub
                End Try
            End If
        Else
            MessageBox.Show("Ban chua chon vi tri xoa!")
        End If
       
    End Sub

    Private Sub frqlthucodinh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbx_thang.SelectedIndex = Now.Month
        Dim nam = Date.Now.Year
        Dim i As Integer = 2
        While i >= 0
            cbx_nam.Items.Add(nam - i)
            i = i - 1
        End While
        cbx_nam.SelectedItem = Date.Now.Year

        lbl_nguoithu.Text = XL_NguoiDung.Lay(Me.Tag).Username

        cbxloaithunhappt.DataSource = XL_LoaiThu.LayDS()
        cbxloaithunhappt.DisplayMember = "Loai"
        cbxloaithunhappt.ValueMember = "ID"

        cbxtaikhoanpt.DataSource = XL_TaiKhoan.LayDS(Me.Tag)
        cbxtaikhoanpt.DisplayMember = "TenTK"
        cbxtaikhoanpt.ValueMember = "ID"
        HienThi()
    End Sub

    Private Sub lsvphieuthu_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsvphieuthu.SelectedIndexChanged
        Try
            Dim p As ThuCoDinh = lsvphieuthu.SelectedItems(0).Tag
            txt_ngaythu.Text = p.Ngay.ToString()
            txtsotienpt.Text = p.SoTien.ToString()
            cbxloaithunhappt.SelectedValue = p.LoaiThu
            cbxtaikhoanpt.SelectedValue = p.TaiKhoan
            rtbchitiet.Text = p.ChiTiet
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub
End Class